/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package PWS;

/**
 *
 * @author LBAOMELM
 */
public class Vigenere {
    
    final String ALFABET = "abcdefghijklmnopqrstuvwxyz";

        String ENCRYPT = null;
        String PASS = null;
        String KEY = null;

        public Vigenere(String e) {
                this.ENCRYPT = e;
        }

        public Vigenere(String e, String p) {
                this.ENCRYPT = e;
                this.PASS = p;
        }

        public void setPass(String p) {
                this.PASS = p;
        }

        public void setEncrypt(String s) {
                this.ENCRYPT = s;
        }

        public String encrypt() {
                String res = "";
                if (ENCRYPT == null || ENCRYPT.length() == 0) {
                        return null;
                }

                if (KEY == null) {
                        KEY = Methods.createKey(ENCRYPT, PASS);
                }

                for (int i = 0; i < ENCRYPT.length(); i++) {
                        res += Constants.ALFABET.charAt(Methods.mod((Methods
                                        .charToNum(ENCRYPT.charAt(i)) + Methods.charToNum(KEY
                                        .charAt(i))), 26));
                }
                return res;
        }

        public String decrypt() {
                String res = "";
                if (ENCRYPT == null || ENCRYPT.length() == 0) {
                        return null;
                }
                
                if (KEY == null) {
                        KEY = Methods.createKey(ENCRYPT, PASS);
                }

                for (int i = 0; i < ENCRYPT.length(); i++) {
                        res += Constants.ALFABET.charAt(Methods.mod((Methods
                                        .charToNum(ENCRYPT.charAt(i)) - Methods.charToNum(KEY
                                        .charAt(i))), 26));
                }
                return res;
        }
    
}
